$(function () {
    //常用jQuery对象
    var studentObserve_datagrid = $("#studentObserve_list");
    var studentObserve_dailog = $("#studentObserve_dialog");
    var emo_form = $("#studentObserve_form");


    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {

            studentObserve_dailog.dialog("open");
            studentObserve_dailog.dialog("setTitle", "新增");
            $("#password").show();
        },

        //编辑按钮方法
        edit: function () {
            var row = studentObserve_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $("#password").hide();
            studentObserve_dailog.dialog("open");
            if (row.dept) {
                row["dept.id"] = row.dept.id;
            }
            studentObserve_dailog.dialog("setTitle", "编辑");
            studentObserve_dailog.form("load", row);
            $.get("/role/getRoleByStudentObserveId.do",{studentObserveId:row.id},function (data) {
                $("#roleId").combobox("setValues",data);
            },"json");


        },


        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            emo_form.form("submit", {
                url: "/studentObserve/saveOrUpdate.do",
                onSubmit: function (param) {
                    var roles = $("#roleId").combobox("getValues");
                    for (var i = 0; i < roles.length; i++) {
                        param["roles[" + i + "].id"] = roles[i];
                    }
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            studentObserve_dailog.dialog("close");
                            studentObserve_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            studentObserve_dailog.dialog("close");
                        });
                    }
                }
            })
        },


        //刷新页面
        reloadList: function () {
            $("#keyword").textbox("setValue", "");
            $("#deptId").combobox("setValue", "");
            var beginDate = $("#beginDate").datebox("setValue", "");
            var endDate = $("#endDate").datebox("setValue", "");
            studentObserve_datagrid.datagrid("reload", {deptId: "-1"});
        },

        //审核
        changeState:function () {
            var row = studentObserve_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            var entity_state = "确认审核吗?";
            $.messager.confirm("温馨提示",entity_state,function (r) {
                if(r){
                    $.get("/studentObserve/changeState.do",{id:row.id,newClass:row.newClass.id},function (data) {
                        if(data.success){
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                studentObserve_datagrid.datagrid("reload");
                            });
                        }
                    })
                }
            })
        },

        //反审核
        unchangeState:function () {
            var row = studentObserve_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            console.log(row);
            var entity_state = "反审核前请你看清楚,实现功能很累的兄dei";
            $.messager.confirm("温馨提示",entity_state,function (r) {
                if(r){
                    $.get("/studentObserve/unchangeState.do",{id:row.id,oldClass:row.oldClass.id},function (data) {
                        if(data.success){
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                studentObserve_datagrid.datagrid("reload");
                            });
                        }
                    })
                }
            })
        },


        //高级查询按钮
        searchs: function () {
            var keyword = $("#keyword").textbox("getValue");
            var classId = $("#classId").combobox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");
            studentObserve_datagrid.datagrid("load", {
                keyword: keyword,
                classId: classId,
                beginDate: beginDate,
                endDate: endDate
            });
        }
    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    studentObserve_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        url: "/studentObserve/query.do",
        singleSelect: "true",
        pagination: true,
        rownumbers: true,
        columns: [[
            {field: 'client', title: '学员姓名', width: 100, formatter: function (client) {
                    return client ? client.name : client;
                }
            },
            {field: 'totalAmount', title: '总学费', width: 100},
            {field: 'waitngfit', title: '待缴学费', width: 100},
            {field: 'payfit', title: '已交学费', width: 100},
            {field: 'stateDate', title: '升班留级时间', width: 100},
            {field: 'tel', title: '联系电话', width: 100},
            {field: 'oldClass', title: '以前的班级', width: 100, formatter: function (oldClz) {
                    return oldClz ? oldClz.name : "<font color='red'>未分配</font>";
                }
            },
            {field: 'newClass', title: '转入的班级', width: 100,formatter: function (newClz) {
                    return newClz ? newClz.name : "<font color='red'>未分配</font>";
                }
            },
            {field: 'saleUser', title: '营销人员', width: 100},
            {field: 'state', title: '审核状态', width: 100, formatter: function (state) {
                    return state ? "<font color='green'>已审核</font>" : "<font color='red'>未审核</font>";
                }
            },
        ]],
        toolbar: "#tools",
        onClickRow: function (index, row) {
            if (row.state) {
                $("#btn_edit").hide();
                $("#btn_changeState").hide();
                $("#btn_unchangeState").show();
            } else {
                $("#btn_edit").show();
                $("#btn_unchangeState").hide();
                $("#btn_changeState").show();
            }
        },
    });

    //编辑添加弹出框设置
    studentObserve_dailog.dialog({
        width: 350,
        height: 350,
        closed: true,
        onClose: function () {
            emo_form.form("clear");
        },
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["saveOrUpdate"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                studentObserve_dailog.dialog("close");
            }
        }]
    });



    //form表单设置
    studentObserve_dailog.form({
        fit: true
    })


});

